
// 评论模块
const sequelize = require('../config/operateDB');
const { Sequelize, DataTypes, Op } = require('sequelize');
const { Product } = require('./product');


const Comment = sequelize.define('comments', {
    id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
    },
    // 商品id
    productId: {
        type: DataTypes.BIGINT,
        allowNull: false,
    },
    // 评论内容
    content: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    // 评论人
    name: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
}, {

})

Comment.sync();

const newsModel = {
    async getComments(ctx) {
        const params = ctx.query;
        // const count = await Comment.count();
        const {offset = 0, limit = 10, productId, name = ''} = params;
        const searchCondition = {
            name: {
                [Op.like]: '%' + name + '%'
            }
        };
        if (productId){
            searchCondition.productId = productId;
        }
        const { count, rows } = await Comment.findAndCountAll(
            {
                where: searchCondition,
                limit: parseInt(limit),
                offset: parseInt(offset),
                raw: true,
            }
        );

        ctx.pageRes({
            limit,
            offset,
            list: rows,
            total: count,
        });
    },

    async addComments(ctx) {
        const params = ctx.request.body;
        const validFlag = this.validComments(ctx, params);
        if (validFlag) {
            const comments = await Comment.create({
                ...params,
            });
            
            ctx.success('评论成功！', comments);
        }
    },
    validComments(ctx, params) {
        if (!params.productId) {
            ctx.errorParams('商品id不存在！');
            return false;
        } else if (!params.content) {
            ctx.errorParams('评论内容不存在！');
            return false;
        } else if (!params.name) {
            ctx.errorParams('评论用户不存在！');
            return false;
        } else {
            return true;
        }
    },
    async deleteComments(ctx)  {
        const deleteId = ctx.request.body.id;
        if (!deleteId) {
            ctx.errorParams('id不存在！');
        } else {
            await Comment.destroy({
                where: {
                    id: deleteId,
                }
            });

            ctx.success('删除成功！', {
                id: deleteId,
            })
        }
        
    },
    // async editComments(ctx)  {
    //     const params = ctx.request.body;
    //     console.log(params.id);
    //     const editId = params.id;
    //     if (!editId) {
    //         ctx.errorParams('id不存在！');
    //     } else {
    //         // 
    //         const news = await Comment.findOne({
    //             where: {
    //                 id: editId
    //             },
    //             raw: true,
    //         });
    //         delete params.id;
    //         await Comment.update({
    //             ...news,
    //             ...params,
    //         }, {
    //             where: {
    //                 id: editId
    //             },
    //         })
            
    //         ctx.success('修改成功', {
    //             id: editId,
    //         });
    //     }
        
    // }
    
};


module.exports = newsModel;